package lzy.service.impl;

import lzy.entity.User;
import lzy.mapper.UserMapper;
import lzy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User findByUsername(String username) {
        return userMapper.findByUsername(username);
    }

    @Override
    public boolean authenticate(String username, String password) {
        User user = findByUsername(username);
        if (user == null) {
            return false;
        }

        // 打印调试信息
        System.out.println("Input password: " + password + ", Length: " + password.length());
        System.out.println("Stored password: " + user.getPassword() + ", Length: " + user.getPassword().length());

        // 直接比对明文密码
        return password.equals(user.getPassword());
    }

    @Override
    public boolean updatePassword(Long id, String newPassword) {
        // 在这里实现密码更新逻辑
        // 由于目前没有密码加密，直接更新即可
        // 如果将来需要加密，这里需要集成PasswordEncoder
        return userMapper.updatePassword(id, newPassword) > 0;
    }

    @Override
    public java.util.List<User> findByRole(String role) {
        return userMapper.findByRole(role);
    }

    @Override
    public int countAll() {
        return userMapper.countAll();
    }

    @Override
    public void addUser(User user) {
        userMapper.addUser(user);
    }

    @Override
    public java.util.List<User> findAll() {
        return userMapper.findAll();
    }

    @Override
    public void deleteUserById(Long id) {
        userMapper.deleteById(id);
    }
} 